/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 draw_terrain.cpp

	$Header: /heroes4/draw_terrain.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "draw_terrain.h"

#include "abstract_bitmap.h"

// ------------------------------------------------------------------------------
// Details namespace
// ------------------------------------------------------------------------------

namespace draw_terrain_details
{

	// ------------------------------------------------------------------------------
	// ------------------------------------------------------------------------------
	void draw_grid(
		t_quad< int > const &	height_quad,
		t_abstract_bitmap16 &	dest,
		t_screen_point const &	screen_pos,
		t_screen_rect const &	clip_rect )
	{
		int tile_left_y = screen_pos.y + k_adventure_tile_height / 2 - height_quad.left;
		int tile_top_y = screen_pos.y - height_quad.top;
		int tile_right_y = screen_pos.y + k_adventure_tile_height / 2 - height_quad.right;

		int tile_left = clip_rect.left;
		if ( tile_left < screen_pos.x )
			tile_left = screen_pos.x;

		int tile_right = clip_rect.right;
		if ( tile_right > screen_pos.x + k_adventure_tile_width )
			tile_right = screen_pos.x + k_adventure_tile_width;

		int tile_x;
		for ( tile_x = tile_left; tile_x < tile_right; ++tile_x )
		{
			int tile_y;
			if ( tile_x < screen_pos.x + k_adventure_tile_width / 2 )
				tile_y = tile_top_y + ( tile_left_y - tile_top_y ) * ( k_adventure_tile_width / 2 - ( tile_x - screen_pos.x ) - 1 ) / ( k_adventure_tile_width / 2 );
			else
				tile_y = tile_top_y + ( tile_right_y - tile_top_y ) * ( ( tile_x - screen_pos.x ) - k_adventure_tile_width / 2 ) / ( k_adventure_tile_width / 2 ); 

			if ( tile_y >= clip_rect.top && tile_y < clip_rect.bottom )
				*( dest.advance_line( dest.get_data_ptr(), tile_y ) + tile_x ) = 0;
		}
	}

}
